package model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;



public class FuncionarioDAO {

	private static PreparedStatement pstmt = null;
    private static ResultSet rs = null;
    
    public static boolean create(Funcionario func) {
        try {
           
            pstmt = ConnectionFabric.getConnection().prepareStatement(
            		 "Insert Into Funcionario(idfuncionario,nome, telefone, endereco, bairro, cep, cidade, estado, cargo, nivelacesso, login, senha) " +
                    "Values(?,?,?,?,?,?,?,?,?,?,?,?)");
            pstmt.setInt(1, func.getIdfuncionario());
            pstmt.setString(2, func.getNome());
            pstmt.setString(3, func.getTelefone());
            pstmt.setString(4, func.getEndereco());
            pstmt.setString(5, func.getBairro());
            pstmt.setString(6, func.getCep());
            pstmt.setString(7, func.getCidade());
            pstmt.setString(8, func.getEstado());
            pstmt.setString(9, func.getCargo());
            pstmt.setInt(10, func.getNivelacesso());
            pstmt.setString(11, func.getLogin());
            pstmt.setString(12, func.getSenha());
            pstmt.executeUpdate();

            pstmt.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public static boolean update(Funcionario func) {
        try {
            pstmt = ConnectionFabric.getConnection().prepareStatement(
                    "Update Funcionario Set Senha=?, Nome=? Where idfuncionario =?");
            pstmt.setString(1, func.getLogin());            
            pstmt.setString(2, func.getSenha());
            pstmt.setInt(5, func.getIdfuncionario());
            pstmt.executeUpdate();
            pstmt.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public static boolean delete(Funcionario func) {
        try {
            pstmt = ConnectionFabric.getConnection().prepareStatement(
                    "Delete From funcionario Where idfuncionario = ?");
            pstmt.setInt(1, func.getIdfuncionario());
            pstmt.executeUpdate();
            pstmt.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public static ArrayList<Funcionario> getAll() {
        try {
            ArrayList<Funcionario> listAll = null;
            Funcionario func = new Funcionario();
            pstmt = ConnectionFabric.getConnection().prepareStatement(
                    "Select * From funcionario Order By Nome");
            rs = pstmt.executeQuery();
            if (rs.next()) {
                listAll = new ArrayList<Funcionario>();
                do {
                    func = new Funcionario();
                    func.setIdFuncionario(rs.getInt("idfuncionario"));
                    func.setNome(rs.getString("Nome"));
                    func.setTelefone(rs.getString("telefone"));                   	           
		            func.setEndereco(rs.getString("endereco"));
		            func.setBairro(rs.getString("bairro"));
		            func.setCep(rs.getString(""));
		            func.setEstado("");
		            func.setCidade("");
		            func.setCargo("");
                    listAll.add(func);
                } while (rs.next());
            }
            rs.close();
            pstmt.close();
            return listAll;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static Funcionario getById(int Id) {
        try {
            Funcionario func = null;
            pstmt = ConnectionFabric.getConnection().prepareStatement(
                    "Select * From func Where idfuncionario = ?");
            pstmt.setInt(1, Id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                func = new Funcionario();
                func.setIdFuncionario(rs.getInt("Codfunc"));
                func.setSenha(rs.getString("Senha"));
                func.setNome(rs.getString("Nome"));
                
            }
            rs.close();
            pstmt.close();
            return func;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static Funcionario getfuncLogin(String login, String senha){
        try {
        	Funcionario func = null;
            
            pstmt = ConnectionFabric.getConnection().prepareStatement(
                    "Select * From funcionario Where  login=? AND Senha=?");
            pstmt.setString(1, login);
            pstmt.setString(2, senha);          
            rs = pstmt.executeQuery();
            if(rs.next()){
                func = new Funcionario();
                func.setLogin(rs.getString("login"));
                func.setSenha(rs.getString("senha"));
                func.setNivelacesso(rs.getInt("nivelacesso"));
               
                
            }
            return func;
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }        
    }
            
    
    
}
